package com.example.demo.dto;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;
import org.springframework.http.HttpStatus;

@Data
@JsonPropertyOrder({ "Code", "success", "message", "data" })
public class ApiResponse<T> {
    private int Code;      // HTTP 状态码
    private boolean success; // 操作是否成功
    private String message;  // 提示信息
    private T data;          // 返回的數據

    private ApiResponse(int Code, boolean success, String message, T data) {
        this.Code = Code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    // --- 基础的成功响应方法 ---
    public static <T> ApiResponse<T> success(HttpStatus status, String message, T data) {
        return new ApiResponse<>(status.value(), true, message, data);
    }

    public static <T> ApiResponse<T> success(HttpStatus status, T data) {
        return new ApiResponse<>(status.value(), true, "操作成功", data);
    }

    public static ApiResponse<Void> successMessage(HttpStatus status, String message) {
        return new ApiResponse<>(status.value(), true, message, null);
    }

    // --- 基础的失败响应方法 ---
    public static <T> ApiResponse<T> error(HttpStatus status, String message) {
        return new ApiResponse<>(status.value(), false, message, null);
    }

    // --- 快速创建特定状态的响应 (辅助方法) ---
    public static <T> ApiResponse<T> created(String message, T data) {
        // 即使是创建，如果要求Code为200，则使用HttpStatus.OK
        return success(HttpStatus.OK, message, data);
    }
    public static <T> ApiResponse<T> created(T data) {
        return success(HttpStatus.OK, "创建成功", data);
    }

    public static <T> ApiResponse<T> ok(String message, T data) {
        return success(HttpStatus.OK, message, data);
    }
    public static <T> ApiResponse<T> ok(T data) {
        return success(HttpStatus.OK, data);
    }

    public static ApiResponse<Void> okMessage(String message) {
        return successMessage(HttpStatus.OK, message);
    }

    public static <T> ApiResponse<T> badRequest(String message) {
        return error(HttpStatus.BAD_REQUEST, message);
    }

    public static <T> ApiResponse<T> unauthorized(String message) {
        return error(HttpStatus.UNAUTHORIZED, message);
    }
    public static <T> ApiResponse<T> forbidden(String message) {
        return error(HttpStatus.FORBIDDEN, message);
    }

    public static <T> ApiResponse<T> notFound(String message) {
        return error(HttpStatus.NOT_FOUND, message);
    }

    public static <T> ApiResponse<T> conflict(String message) {
        return error(HttpStatus.CONFLICT, message);
    }

    public static <T> ApiResponse<T> internalServerError(String message) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR, message);
    }
}
